//
//  WmcUploadFailureCell.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/4/2.
//

import UIKit

class WmcUploadFailureCell: UITableViewCell {
    // Lifecycle

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.setupUI()
        self.makeConstraints()
        self.bindEvent()
    }

    @available(*, unavailable)
    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // Internal

    /// 取消任务
    var cancelTaskClosure: ((WmcUploadImageV2Model) -> Void)?
    /// 重试
    var retryTaskClosure: ((WmcUploadImageV2Model) -> Void)?

    private(set) var model: WmcUploadImageV2Model?

    func bindModel(_ taskModel: WmcUploadImageV2Model) {
        model = taskModel
        
        let filePath = YPSystem.Documents.Path.yp.appendPathComponent(taskModel.previewImagePath)
        
        photoImageView.image = UIImage(contentsOfFile: filePath)
        if taskModel.uploading {
            statusLabel.text = "同步中..."
            cancelBtn.isHidden = true
            retryBtn.isHidden = true
        } else {
            statusLabel.text = "同步失败"
            cancelBtn.isHidden = false
            retryBtn.isHidden = false
        }
        
        if let params = WmcMediaUploadImageParameterModel.deserialize(from: taskModel.parameters) {
            videoMarkView.isHidden = params.type == 0
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    // Private

    private lazy var cancelBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.bounds = CGRect(x: 0, y: 0, width: 30, height: 46)
        btn.setImage("close".wmc_image, for: .normal)
        btn.setTitle("取消", for: .normal)
        btn.setTitleColor(.wmc_grayH6_75757A, for: .normal)
        btn.titleLabel?.font = 14.yp.uiFont(weight: .regular)

        let imageSize = btn.imageView?.frame.size ?? CGSize.zero
        let labelSize = btn.titleLabel?.intrinsicContentSize ?? CGSize.zero
        let imageEdgeInsets = UIEdgeInsets(top: -labelSize.height - 5, left: 0, bottom: 0, right: -labelSize.width)
        let titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageSize.width, bottom: -imageSize.height - 5, right: -0)
        btn.imageEdgeInsets = imageEdgeInsets
        btn.titleEdgeInsets = titleEdgeInsets

        return btn
    }()

    private lazy var retryBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.bounds = CGRect(x: 0, y: 0, width: 30, height: 46)
        btn.setImage("upload_update_icon".wmc_image, for: .normal)
        btn.setTitle("重试", for: .normal)
        btn.setTitleColor(.wmc_grayH6_75757A, for: .normal)
        btn.titleLabel?.font = 14.yp.uiFont(weight: .regular)

        let imageSize = btn.imageView?.frame.size ?? CGSize.zero
        let labelSize = btn.titleLabel?.intrinsicContentSize ?? CGSize.zero
        let imageEdgeInsets = UIEdgeInsets(top: -labelSize.height - 5, left: 0, bottom: 0, right: -labelSize.width)
        let titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageSize.width, bottom: -imageSize.height - 5, right: -0)
        btn.imageEdgeInsets = imageEdgeInsets
        btn.titleEdgeInsets = titleEdgeInsets

        return btn
    }()

    private lazy var photoImageView: UIImageView = {
        let view = UIImageView()
        return view
    }()

    private lazy var statusLabel: UILabel = {
        let label = UILabel()
        label.textColor = .wmc_grayH2_323233
        label.font = 17.yp.uiFont
        label.text = "同步失败"
        return label
    }()

    private lazy var line: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_grayH20_F7F7F7
        return view
    }()
    
    private lazy var videoMarkView: UIImageView = {
        let view = UIImageView(image: "player_play_center".wmc_image)
        view.isHidden = true
        return view
    }()

    private func setupUI() {
        contentView.addSubview(photoImageView)
        contentView.addSubview(statusLabel)
        contentView.addSubview(cancelBtn)
        contentView.addSubview(retryBtn)
        contentView.addSubview(line)
        photoImageView.addSubview(videoMarkView)
    }

    private func makeConstraints() {
        photoImageView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(50)
        }

        statusLabel.snp.makeConstraints { make in
            make.left.equalTo(photoImageView.snp.right).offset(16)
            make.centerY.equalToSuperview()
        }

        cancelBtn.snp.makeConstraints { make in
            make.right.equalTo(retryBtn.snp.left).offset(-24)
            make.centerY.equalToSuperview()
            make.width.equalTo(30)
            make.height.equalTo(46)
        }

        retryBtn.snp.makeConstraints { make in
            make.right.equalTo(-16)
            make.centerY.equalToSuperview()
            make.width.equalTo(30)
            make.height.equalTo(46)
        }

        line.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        videoMarkView.snp.makeConstraints { make in
            make.width.height.equalTo(12)
            make.centerX.centerY.equalToSuperview()
        }
    }

    private func bindEvent() {
        cancelBtn.rx.controlEvent(.touchUpInside).subscribe { [weak self] _ in
            guard let weakSelf = self else { return }
            if let model = weakSelf.model {
                weakSelf.cancelTaskClosure?(model)
            }
        }.disposed(by: rx.disposeBag)

        retryBtn.rx.controlEvent(.touchUpInside).subscribe { [weak self] _ in
            guard let weakSelf = self else { return }
            if let model = weakSelf.model {
                weakSelf.retryTaskClosure?(model)
            }
        }.disposed(by: rx.disposeBag)
    }
}
